package com.sy.protocal;

import com.sy.messages.LoginRequestMessage;
import com.sy.protocal.MessageCodec;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * @Author yumax
 * @Date 2024 01 16 11 03
 **/
public class TestMessageCodec {

    public static void main(String[] args) throws Exception {
        final EmbeddedChannel channel = new EmbeddedChannel(
                new LengthFieldBasedFrameDecoder(1024,11,4,0,0),
                new LoggingHandler(),
                new MessageCodec());

        //测试encode方法
        final LoginRequestMessage loginRequestMessage = new LoginRequestMessage("zhangsan", "123qwe");
        channel.writeOutbound(loginRequestMessage);

        //decode
        final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
        new MessageCodec().encode(null, loginRequestMessage, buf);

        final ByteBuf buf1 = buf.slice(0, 100);
        final ByteBuf buf2 = buf.slice(100, buf.readableBytes() - 100);
        buf1.retain();

        //入站
        channel.writeInbound(buf1);
        channel.writeInbound(buf2);

    }
}
